home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0022_Factoral Program.pas < prev    next >
Pascal/Delphi Source File  |  1993-06-22  |  5KB  |  147 lines

  1. PROGRAM Fact;
  2. {************************************************
  3. * FACTOR - Lookup table demonstration using the        *
  4. * factorial series.                                                                      *
  5. *                                               *
  6. *************************************************}
  7.  
  8. {$N+,E+}     {Set so you can use other real types}
  9. USES Crt,Dos,Timer;   { t1Start, t1Get, t1Format }
  10. CONST
  11.    BigFact = 500;  {largest factorial is for 1754}
  12. TYPE   {defined type for file definition later}
  13.     TableType = ARRAY [0..BigFact] OF Extended;
  14. VAR
  15.    Table : TableType;
  16.  
  17. {************************************************
  18. * factorial - compute the factorial of a number        *
  19. *                                               *
  20. * INP:        i - the # to compute the factorial of        *
  21. * OUT:        The factorial of the number, unless a        *
  22. *                number greater than BIG_FACT or less      *
  23. *                than zero is passed in (which results     *
  24. *                in 0.0).                                  *
  25. *************************************************}
  26.  
  27. FUNCTION Factorial(I: Integer): Extended;
  28. VAR
  29.    K : Integer;
  30.         F : Extended;
  31. BEGIN
  32.         IF I = 0 THEN
  33.                 F := 1
  34.         ELSE
  35.        BEGIN
  36.           IF (I > 0) AND (I <= BigFact) THEN
  37.              BEGIN
  38.                 F := 1;
  39.                 FOR K := 1 TO I DO
  40.                    F := F * K
  41.              END
  42.           ELSE
  43.              F := 0
  44.        END;
  45.         Factorial := F
  46. END;
  47.  
  48. {************************************************
  49. * Main - generate & save table of factorials    *
  50. *************************************************}
  51.  
  52. VAR
  53.    I, J, N            : Integer;
  54.    F                  : Extended;
  55.    T1, T2, T3         : Longint;
  56.    Facts              : FILE OF TableType;
  57. BEGIN
  58.         { STEP 1 - compute each factorial 5 times }
  59.    ClrScr;
  60.         WriteLn('Now computing each factorial 5 times');
  61.         T1 := tStart;
  62.         FOR I :=0 TO 4 DO
  63.                 FOR J := 0 TO BigFact DO
  64.                         F := Factorial(J);              { f=j! }
  65.         T2 := tGet;
  66.         WriteLn('Computing all factorials from 0..n ');
  67.         WriteLn('5 times took ',tFormat(T1,T2),
  68.                 ' secs.');
  69.    WriteLn;
  70.         { STEP 2 - compute the table, then look up
  71.                                  each factorial 5 times.                        }
  72.         WriteLn('Now compute table and look up each ',
  73.                 'factorial 5 times.');
  74.         T1 := tStart;
  75.         FOR I := 0 TO BigFact DO
  76.                 Table[I] := Factorial(I);
  77.         T2 := tGet;
  78.         FOR I := 0 TO 4 DO
  79.                 FOR J :=0 TO BigFact DO
  80.                         F := Table[J]; { f=j! }
  81.         T3 := tGet;
  82.         WriteLn('Computing table took ',tFormat(T1,T2),
  83.                 ' seconds');
  84.         WriteLn('Looking up each factorial 5 times to',
  85.            'ok ',tFormat(T2,T3),' seconds');
  86.         WriteLn('Total: ',tFormat(T1,T3),' seconds');
  87.    WriteLn;
  88. {STEP 3 - Compute each factorial as it is needed}
  89.         WriteLn('Clearing the table,',
  90.                 ' and computing each ');
  91.         WriteLn('factorial as it is needed',
  92.                 ' (for 5) lookups.');
  93.    WriteLn;
  94.         T1 := tStart;
  95.         FOR I := 0 TO BigFact DO
  96.                 Table[I] := -1;            { unknown Val }
  97.         FOR I := 0 TO 4 DO
  98.                 FOR J := 0 TO BigFact DO
  99.            BEGIN
  100.                         F := Table[J];
  101.                         IF F < 0 THEN
  102.                                 BEGIN
  103.                                   F := Factorial(J);
  104.                         Table[J] := F    { F = J! }
  105.                 END
  106.            END;
  107.         T2 := tGet;
  108.         WriteLn('Clearing table and computing each');
  109.         WriteLn(' factorial as it was needed for 5');
  110.    WriteLn('lookups took ',tFormat(T1,T2),
  111.            ' secs.');
  112.         { STEP 4 - write the table to disk (we are
  113.      not timing this step, because if you are
  114.      loading it from disk,        you presumably do not
  115.      care how long it took to compute it.      }
  116.    Assign(Facts,'Fact_tbl.tmp');
  117.    Rewrite(Facts);
  118.    Write(Facts,Table);
  119.         Close(Facts);
  120.         { Flush the disk buffer, so that the time
  121.           is not affected by having the data in a
  122.           disk buffer.                                                                }
  123.         Exec('C:\COMMAND.COM','/C CHKDSK');
  124.         { STEP 5 - read the table from disk, and
  125.                                  use each factorial 5 times                }
  126.         T1 := tStart;
  127.    Assign(Facts,'Fact_tbl.TMP');
  128.    Reset(Facts);
  129.    Read(Facts,Table);
  130.    Close(Facts);
  131.         T2 := tGet;
  132.         FOR I := 0 TO 4 DO
  133.                 FOR J :=0 TO BigFact DO
  134.            F := Table[J];                 { f=j! }
  135.         T3 := tGet;
  136.         WriteLn('Reading the Table from disk took ',
  137.                         tFormat(T1,T2),' seconds.');
  138.         WriteLn('Looking up each Factorial 5 times ',
  139.         'to ok took ',tFormat(T2,T3),' seconds.');
  140.         WriteLn('Total: ',tFormat(T1,T3),' seconds.');
  141.    WriteLn;
  142.    WriteLn('Press Enter TO see the factorials');
  143.    ReadLN;
  144.    FOR I:=0 TO BigFact DO
  145.       WriteLn('[',I,'] = ',Table[I]);
  146. end.
  147.